subroutine bpdecode300(llr,apmask,maxiterations,decoded,niterations,cw)

! A log-domain belief propagation decoder for the (300,60) code.

integer, parameter:: N=300, K=60, M=N-K
integer*1 codeword(N),cw(N),apmask(N)
integer  colorder(N)
integer*1 decoded(K)
integer Nm(5,M)  ! 4, or 5 bits per check 
integer Mn(7,N)  ! 2, 3, or 7 checks per bit
integer synd(M)
real tov(7,N)
real toc(5,M)
real tanhtoc(5,M)
real zn(N)
real llr(N)
real Tmn
integer nrw(M)
integer ncw(N)

data colorder/    &
0,1,2,3,4,5,6,7,8,9,10,11,123,12,13,14,15,16,17,18, &
19,20,21,22,23,24,25,138,26,145,27,28,29,30,31,32,33,34,35,36, &
37,154,38,39,40,41,42,43,44,144,46,47,48,49,50,51,52,53,143,54, &
125,56,57,58,124,59,120,140,157,160,55,60,61,62,156,162,141,64,65,153, &
181,183,66,170,67,68,69,130,70,164,71,72,73,74,75,63,76,77,135,78, &
79,80,176,169,82,83,84,167,180,85,136,158,129,166,175,142,134,146,121,165, &
88,89,192,90,45,91,92,93,182,189,94,95,96,173,81,97,98,178,122,126, &
132,99,100,152,186,193,101,102,151,103,104,172,159,168,150,190,147,148,201,107, &
205,177,108,198,197,174,127,109,185,110,202,87,199,171,179,187,139,137,106,131, &
206,194,112,149,155,113,128,184,196,86,114,203,212,195,208,105,188,161,163,191, &
200,209,214,204,115,218,133,111,207,117,213,216,211,217,116,215,219,220,210,221, &
118,222,223,225,224,228,226,229,231,227,233,119,234,235,232,230,237,239,236,238, &
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259, &
260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279, &
280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299/

data Mn/               &
   1,  67,   0,   0,   0,   0,   0, &
   2, 189,   0,   0,   0,   0,   0, &
   3, 201,   0,   0,   0,   0,   0, &
   4,  13,   0,   0,   0,   0,   0, &
   5,  84,   0,   0,   0,   0,   0, &
   6, 188,   0,   0,   0,   0,   0, &
   7, 140,   0,   0,   0,   0,   0, &
   8, 167,   0,   0,   0,   0,   0, &
   9, 187,   0,   0,   0,   0,   0, &
  10, 173,   0,   0,   0,   0,   0, &
  11,  88,   0,   0,   0,   0,   0, &
  12, 213,   0,   0,   0,   0,   0, &
  14, 141,   0,   0,   0,   0,   0, &
  15, 236,   0,   0,   0,   0,   0, &
  16, 117,   0,   0,   0,   0,   0, &
  17,  99,   0,   0,   0,   0,   0, &
  18, 111,   0,   0,   0,   0,   0, &
  19, 178,   0,   0,   0,   0,   0, &
  20,  28,   0,   0,   0,   0,   0, &
  21, 177,   0,   0,   0,   0,   0, &
  22, 199,   0,   0,   0,   0,   0, &
  23, 209,   0,   0,   0,   0,   0, &
  24, 220,   0,   0,   0,   0,   0, &
  25,  59,   0,   0,   0,   0,   0, &
  26, 224,   0,   0,   0,   0,   0, &
  27,  30,   0,   0,   0,   0,   0, &
  29, 157,   0,   0,   0,   0,   0, &
  31, 184,   0,   0,   0,   0,   0, &
  32, 179,   0,   0,   0,   0,   0, &
  33, 149,   0,   0,   0,   0,   0, &
  34, 144,   0,   0,   0,   0,   0, &
  35,  80,   0,   0,   0,   0,   0, &
  36, 228,   0,   0,   0,   0,   0, &
  37, 185,   0,   0,   0,   0,   0, &
  38, 197,   0,   0,   0,   0,   0, &
  39,  69,   0,   0,   0,   0,   0, &
  40,  42,   0,   0,   0,   0,   0, &
  41, 112,   0,   0,   0,   0,   0, &
  43,  70,   0,   0,   0,   0,   0, &
  44, 198,   0,   0,   0,   0,   0, &
  45,  76,   0,   0,   0,   0,   0, &
  46,  68,   0,   0,   0,   0,   0, &
  47,  90,   0,   0,   0,   0,   0, &
  48,  75,   0,   0,   0,   0,   0, &
  49, 118,   0,   0,   0,   0,   0, &
  50, 125,   0,   0,   0,   0,   0, &
  51, 114,   0,   0,   0,   0,   0, &
  52, 239,   0,   0,   0,   0,   0, &
  53, 108,   0,   0,   0,   0,   0, &
  54, 120,   0,   0,   0,   0,   0, &
  55, 162,   0,   0,   0,   0,   0, &
  56, 218,   0,   0,   0,   0,   0, &
  57, 138,   0,   0,   0,   0,   0, &
  58, 212,   0,   0,   0,   0,   0, &
  60, 207,   0,   0,   0,   0,   0, &
  61,  71,   0,   0,   0,   0,   0, &
  62,  65,   0,   0,   0,   0,   0, &
  63, 161,   0,   0,   0,   0,   0, &
  64, 166,   0,   0,   0,   0,   0, &
  66, 158,   0,   0,   0,   0,   0, &
  72, 235,   0,   0,   0,   0,   0, &
  73, 225,   0,   0,   0,   0,   0, &
  74, 116,   0,   0,   0,   0,   0, &
  77,  96,   0,   0,   0,   0,   0, &
  78,  81,   0,   0,   0,   0,   0, &
  79,  82,   0,   0,   0,   0,   0, &
  83, 229,   0,   0,   0,   0,   0, &
  85, 134,   0,   0,   0,   0,   0, &
  86, 176,   0,   0,   0,   0,   0, &
  87, 203,   0,   0,   0,   0,   0, &
  89, 145,   0,   0,   0,   0,   0, &
  91, 152,   0,   0,   0,   0,   0, &
  92, 237,   0,   0,   0,   0,   0, &
  93, 215,   0,   0,   0,   0,   0, &
  94, 130,   0,   0,   0,   0,   0, &
  95, 156,   0,   0,   0,   0,   0, &
  97, 104,   0,   0,   0,   0,   0, &
  98, 182,   0,   0,   0,   0,   0, &
 100, 222,   0,   0,   0,   0,   0, &
 101, 123,   0,   0,   0,   0,   0, &
 102, 181,   0,   0,   0,   0,   0, &
 103, 135,   0,   0,   0,   0,   0, &
 105, 146,   0,   0,   0,   0,   0, &
 106, 115,   0,   0,   0,   0,   0, &
 107, 109,   0,   0,   0,   0,   0, &
 110, 194,   0,   0,   0,   0,   0, &
 113, 164,   0,   0,   0,   0,   0, &
 119, 172,   0,   0,   0,   0,   0, &
 121, 190,   0,   0,   0,   0,   0, &
 122, 169,   0,   0,   0,   0,   0, &
 124, 211,   0,   0,   0,   0,   0, &
 126, 165,   0,   0,   0,   0,   0, &
 127, 139,   0,   0,   0,   0,   0, &
 128, 129,   0,   0,   0,   0,   0, &
 131, 205,   0,   0,   0,   0,   0, &
 132, 196,   0,   0,   0,   0,   0, &
 133, 193,   0,   0,   0,   0,   0, &
 136, 200,   0,   0,   0,   0,   0, &
 137, 159,   0,   0,   0,   0,   0, &
 142, 204,   0,   0,   0,   0,   0, &
 143, 154,   0,   0,   0,   0,   0, &
 147, 238,   0,   0,   0,   0,   0, &
 148, 175,   0,   0,   0,   0,   0, &
 150, 216,   0,   0,   0,   0,   0, &
 151, 171,   0,   0,   0,   0,   0, &
 153, 231,   0,   0,   0,   0,   0, &
 155, 208,   0,   0,   0,   0,   0, &
 160, 230,   0,   0,   0,   0,   0, &
 163, 223,   0,   0,   0,   0,   0, &
 168, 217,   0,   0,   0,   0,   0, &
 170, 180,   0,   0,   0,   0,   0, &
 174, 233,   0,   0,   0,   0,   0, &
 183, 202,   0,   0,   0,   0,   0, &
 186, 214,   0,   0,   0,   0,   0, &
 191, 206,   0,   0,   0,   0,   0, &
 192, 219,   0,   0,   0,   0,   0, &
 195, 227,   0,   0,   0,   0,   0, &
 210, 226,   0,   0,   0,   0,   0, &
 221, 234,   0,   0,   0,   0,   0, &
 232, 240,   0,   0,   0,   0,   0, &
   1, 106,   0,   0,   0,   0,   0, &
   2, 119,   0,   0,   0,   0,   0, &
   3, 139,   0,   0,   0,   0,   0, &
   4,  14,   0,   0,   0,   0,   0, &
   5,  65,   0,   0,   0,   0,   0, &
   6,  61,   0,   0,   0,   0,   0, &
   7, 223,   0,   0,   0,   0,   0, &
   8, 171,   0,   0,   0,   0,   0, &
   9, 136,   0,   0,   0,   0,   0, &
  10, 113,   0,   0,   0,   0,   0, &
  11, 104,   0,   0,   0,   0,   0, &
  12, 175,   0,   0,   0,   0,   0, &
  13, 203,   0,   0,   0,   0,   0, &
  15, 149,   0,   0,   0,   0,   0, &
  16, 226,   0,   0,   0,   0,   0, &
  17, 219,   0,   0,   0,   0,   0, &
  18,  98,   0,   0,   0,   0,   0, &
  19, 211,   0,   0,   0,   0,   0, &
  20,  49,   0,   0,   0,   0,   0, &
  21, 214,   0,   0,   0,   0,   0, &
  22,  68,   0,   0,   0,   0,   0, &
  23,  77,   0,   0,   0,   0,   0, &
  24, 116,   0,   0,   0,   0,   0, &
  25, 235,   0,   0,   0,   0,   0, &
  26,  50,   0,   0,   0,   0,   0, &
  27, 124,   0,   0,   0,   0,   0, &
  28, 229,   0,   0,   0,   0,   0, &
  29,  83,   0,   0,   0,   0,   0, &
  30, 158,   0,   0,   0,   0,   0, &
  31, 220,   0,   0,   0,   0,   0, &
  32, 155,   0,   0,   0,   0,   0, &
  33, 152,   0,   0,   0,   0,   0, &
  34, 231,   0,   0,   0,   0,   0, &
  35, 207,   0,   0,   0,   0,   0, &
  36,  40,   0,   0,   0,   0,   0, &
  37, 142,   0,   0,   0,   0,   0, &
  38,  75,   0,   0,   0,   0,   0, &
  39,  90, 167,   0,   0,   0,   0, &
  41,  55, 125,   0,   0,   0,   0, &
  42, 153, 196,   0,   0,   0,   0, &
  43,  72, 112,   0,   0,   0,   0, &
  44, 183, 233,   0,   0,   0,   0, &
  45,  81, 178,   0,   0,   0,   0, &
  46, 187, 230,   0,   0,   0,   0, &
  47, 133, 176,   0,   0,   0,   0, &
  48,  54, 186,   0,   0,   0,   0, &
  51, 150, 224,   0,   0,   0,   0, &
  52,  53, 190,   0,   0,   0,   0, &
  56, 143, 228,   0,   0,   0,   0, &
  57,  97, 197,   0,   0,   0,   0, &
  58,  62,  89,   0,   0,   0,   0, &
  59, 174, 194,   0,   0,   0,   0, &
  60,  91,  93,   0,   0,   0,   0, &
  63,  85,  96,   0,   0,   0,   0, &
  64,  92, 205,   0,   0,   0,   0, &
  66,  67, 164,   0,   0,   0,   0, &
  69, 103, 159,   0,   0,   0,   0, &
  70, 117, 122,   0,   0,   0,   0, &
  71,  88, 160,   0,   0,   0,   0, &
  73, 148, 180,   0,   0,   0,   0, &
  74, 108, 109,   0,   0,   0,   0, &
  76, 102, 151,   0,   0,   0,   0, &
  78, 128, 206,   0,   0,   0,   0, &
  79, 215, 239,   0,   0,   0,   0, &
  80, 138, 221,   0,   0,   0,   0, &
  82, 162, 195,   0,   0,   0,   0, &
  84, 161, 184,   0,   0,   0,   0, &
  86, 213, 218,   0,   0,   0,   0, &
  87, 120, 240,   0,   0,   0,   0, &
  94, 100, 157,   0,   0,   0,   0, &
  95, 202, 217,   0,   0,   0,   0, &
  99, 199, 201,   0,   0,   0,   0, &
 101, 127, 225,   0,   0,   0,   0, &
 105, 168, 185,   0,   0,   0,   0, &
 107, 182, 237,   0,   0,   0,   0, &
 110, 147, 208,   0,   0,   0,   0, &
 111, 118, 172,   0,   0,   0,   0, &
 114, 140, 165,   0,   0,   0,   0, &
 115, 130, 141,   0,   0,   0,   0, &
 121, 144, 173,   0,   0,   0,   0, &
 123, 204, 209,   0,   0,   0,   0, &
 126, 137, 188,   0,   0,   0,   0, &
 129, 179, 189,   0,   0,   0,   0, &
 131, 192, 210,   0,   0,   0,   0, &
 132, 200, 238,   0,   0,   0,   0, &
 134, 177, 191,   0,   0,   0,   0, &
 135, 145, 222,   0,   0,   0,   0, &
 146, 229, 236,   0,   0,   0,   0, &
 154, 169, 232,   0,   0,   0,   0, &
 124, 156, 163,   0,   0,   0,   0, &
 166, 223, 234,   0,   0,   0,   0, &
   1,  11, 170,   0,   0,   0,   0, &
   3, 181, 227,   0,   0,   0,   0, &
 193, 198, 220,   0,   0,   0,   0, &
  10,  16, 212,   0,   0,   0,   0, &
  42,  96, 216,   0,   0,   0,   0, &
   2,   6, 215,   0,   0,   0,   0, &
   4, 208, 219,   0,   0,   0,   0, &
   5,  22,  35,   0,   0,   0,   0, &
   7,  12,  20,   0,   0,   0,   0, &
   8,  15,  75,   0,   0,   0,   0, &
   9,  74,  83,   0,   0,   0,   0, &
  13,  37,  50,   0,   0,   0,   0, &
  14,  52,  86,   0,   0,   0,   0, &
  17,  30, 177,   0,   0,   0,   0, &
  18,  25,  97,   0,   0,   0,   0, &
  19,  72, 157,   0,   0,   0,   0, &
  21,  58, 116,   0,   0,   0,   0, &
  23, 111, 226,   0,   0,   0,   0, &
  24,  26, 180,   0,   0,   0,   0, &
  27,  34,  39,   0,   0,   0,   0, &
  28,  32, 161,   0,   0,   0,   0, &
  29,  36,  60,   0,   0,   0,   0, &
  31,  76, 154,   0,   0,   0,   0, &
  33, 101, 238,   0,   0,   0,   0, &
  38,  95, 162,   0,   0,   0,   0, &
  40, 164, 183,   0,   0,   0,   0, &
  41,  92, 196,   0,   0,   0,   0, &
  43,  48,  99, 165, 190, 198, 204, &
  44, 129, 138, 145, 160, 203, 237, &
  45,  65,  66,  98, 127, 137, 146, &
  46, 131, 149, 181, 211, 218, 224, &
  47,  49,  55, 191, 194, 207, 232, &
  51,  69, 106, 109, 119, 184, 217, &
  53,  62, 104, 155, 166, 206, 231, &
  54,  61,  63,  73, 118, 151, 163, &
  56,  94, 110, 117, 185, 189, 214, &
  57,  81,  91, 115, 173, 175, 227, &
  59,  79, 103, 136, 171, 201, 212, &
  24,  64,  77,  93, 202, 235, 236, &
  67, 132, 142, 150, 156, 176, 222, &
  68, 153, 159, 169, 170, 186, 221, &
  70,  84,  89, 113, 174, 197, 205, &
  71, 125, 130, 140, 158, 200, 210, &
   8,  78, 143, 182, 192, 193, 216, &
  23,  80,  82,  90, 108, 139, 228, &
  85, 122, 123, 128, 141, 187, 188, &
  25,  87, 100, 152, 209, 213, 234, &
  88, 134, 147, 167, 172, 178, 239, &
  18,  40, 102, 114, 133, 144, 179, &
   4, 105, 108, 112, 148, 230, 240, &
  29,  33,  50,  62, 107, 195, 199, &
   3,  83, 113, 120, 126, 177, 216, &
  11,  55, 116, 121, 135, 168, 225, &
   1,  27,  28,  76, 187, 226, 233, &
   2,   4,   7,  10,  22,  75, 222, &
   5,  30, 131, 152, 156, 168, 215, &
   6,  13,  19,  58, 196, 228, 229, &
   9,  26, 144, 147, 158, 223, 240, &
  12,  31,  66,  79,  92,  96, 155, &
  14,  54, 103, 173, 202, 232, 238, &
  15,  17,  37,  69, 129, 164, 209, &
  16,  72,  91, 114, 163, 169, 237, &
  20,  45,  89,  99, 143, 180, 208, &
  21,  39,  60, 141, 171, 198, 234, &
  21,  32,  52,  78,  95, 148, 199, &
  34,  73,  84, 157, 200, 221, 236, &
  35,  36,  63,  97, 105, 119, 220, &
  38,  46,  93, 111, 136, 191, 203, &
  41,  51, 151, 160, 213, 214, 231, &
  42,  57,  65, 161, 167, 194, 204, &
  43, 109, 162, 175, 189, 210, 212, &
  44,  74, 100, 149, 170, 188, 197, &
  47,  64,  88, 107, 122, 165, 211, &
  48, 139, 179, 184, 218, 233, 239, &
  49,  94, 106, 112, 138, 142, 205, &
  53,  59, 102, 115, 134, 182, 225, &
  56,  68, 101, 150, 166, 178, 207, &
  61, 117, 126, 154, 195, 219, 224, &
  67,  80, 118, 174, 185, 190, 235, &
  70,  77,  86, 125, 153, 172, 193, &
  32,  71,  87,  90,  98, 110, 135, &
  41,  75,  81,  85, 124, 133, 201, &
  82, 120, 128, 140, 159, 176, 183, &
  22,  72, 104, 130, 146, 181, 217, &
  25,  89,  96, 121, 132, 186, 230, &
 118, 123, 145, 192, 196, 227, 240, &
   1,  14,  35,  38, 114, 127, 192, &
   7,  23,  43,  63, 116, 137, 206, &
   2,  37,  52,  57,  64,  76, 120/

data Nm/               &
   1, 121, 212, 265, 298, &
   2, 122, 217, 266, 300, &
   3, 123, 213, 263,   0, &
   4, 124, 218, 261, 266, &
   5, 125, 219, 267,   0, &
   6, 126, 217, 268,   0, &
   7, 127, 220, 266, 299, &
   8, 128, 221, 255,   0, &
   9, 129, 222, 269,   0, &
  10, 130, 215, 266,   0, &
  11, 131, 212, 264,   0, &
  12, 132, 220, 270,   0, &
   4, 133, 223, 268,   0, &
  13, 124, 224, 271, 298, &
  14, 134, 221, 272,   0, &
  15, 135, 215, 273,   0, &
  16, 136, 225, 272,   0, &
  17, 137, 226, 260,   0, &
  18, 138, 227, 268,   0, &
  19, 139, 220, 274,   0, &
  20, 140, 228, 275, 276, &
  21, 141, 219, 266, 295, &
  22, 142, 229, 256, 299, &
  23, 143, 230, 250,   0, &
  24, 144, 226, 258, 296, &
  25, 145, 230, 269,   0, &
  26, 146, 231, 265,   0, &
  19, 147, 232, 265,   0, &
  27, 148, 233, 262,   0, &
  26, 149, 225, 267,   0, &
  28, 150, 234, 270,   0, &
  29, 151, 232, 276, 292, &
  30, 152, 235, 262,   0, &
  31, 153, 231, 277,   0, &
  32, 154, 219, 278, 298, &
  33, 155, 233, 278,   0, &
  34, 156, 223, 272, 300, &
  35, 157, 236, 279, 298, &
  36, 158, 231, 275,   0, &
  37, 155, 237, 260,   0, &
  38, 159, 238, 280, 293, &
  37, 160, 216, 281,   0, &
  39, 161, 239, 282, 299, &
  40, 162, 240, 283,   0, &
  41, 163, 241, 274,   0, &
  42, 164, 242, 279,   0, &
  43, 165, 243, 284,   0, &
  44, 166, 239, 285,   0, &
  45, 139, 243, 286,   0, &
  46, 145, 223, 262,   0, &
  47, 167, 244, 280,   0, &
  48, 168, 224, 276, 300, &
  49, 168, 245, 287,   0, &
  50, 166, 246, 271,   0, &
  51, 159, 243, 264,   0, &
  52, 169, 247, 288,   0, &
  53, 170, 248, 281, 300, &
  54, 171, 228, 268,   0, &
  24, 172, 249, 287,   0, &
  55, 173, 233, 275,   0, &
  56, 126, 246, 289,   0, &
  57, 171, 245, 262,   0, &
  58, 174, 246, 278, 299, &
  59, 175, 250, 284, 300, &
  57, 125, 241, 281,   0, &
  60, 176, 241, 270,   0, &
   1, 176, 251, 290,   0, &
  42, 141, 252, 288,   0, &
  36, 177, 244, 272,   0, &
  39, 178, 253, 291,   0, &
  56, 179, 254, 292,   0, &
  61, 161, 227, 273, 295, &
  62, 180, 246, 277,   0, &
  63, 181, 222, 283,   0, &
  44, 157, 221, 266, 293, &
  41, 182, 234, 265, 300, &
  64, 142, 250, 291,   0, &
  65, 183, 255, 276,   0, &
  66, 184, 249, 270,   0, &
  32, 185, 256, 290,   0, &
  65, 163, 248, 293,   0, &
  66, 186, 256, 294,   0, &
  67, 148, 222, 263,   0, &
   5, 187, 253, 277,   0, &
  68, 174, 257, 293,   0, &
  69, 188, 224, 291,   0, &
  70, 189, 258, 292,   0, &
  11, 179, 259, 284,   0, &
  71, 171, 253, 274, 296, &
  43, 158, 256, 292,   0, &
  72, 173, 248, 273,   0, &
  73, 175, 238, 270,   0, &
  74, 173, 250, 279,   0, &
  75, 190, 247, 286,   0, &
  76, 191, 236, 276,   0, &
  64, 174, 216, 270, 296, &
  77, 170, 226, 278,   0, &
  78, 137, 241, 292,   0, &
  16, 192, 239, 274,   0, &
  79, 190, 258, 283,   0, &
  80, 193, 235, 288,   0, &
  81, 182, 260, 287,   0, &
  82, 177, 249, 271,   0, &
  77, 131, 245, 295,   0, &
  83, 194, 261, 278,   0, &
  84, 121, 244, 286,   0, &
  85, 195, 262, 284,   0, &
  49, 181, 256, 261,   0, &
  85, 181, 244, 282,   0, &
  86, 196, 247, 292,   0, &
  17, 197, 229, 279,   0, &
  38, 161, 261, 286,   0, &
  87, 130, 253, 263,   0, &
  47, 198, 260, 273, 298, &
  84, 199, 248, 287,   0, &
  63, 143, 228, 264, 299, &
  15, 178, 247, 289,   0, &
  45, 197, 246, 290, 297, &
  88, 122, 244, 278,   0, &
  50, 189, 263, 294, 300, &
  89, 200, 264, 296,   0, &
  90, 178, 257, 284,   0, &
  80, 201, 257, 297,   0, &
  91, 146, 210, 293,   0, &
  46, 159, 254, 291,   0, &
  92, 202, 263, 289,   0, &
  93, 193, 241, 298,   0, &
  94, 183, 257, 294,   0, &
  94, 203, 240, 272,   0, &
  75, 199, 254, 295,   0, &
  95, 204, 242, 267,   0, &
  96, 205, 251, 296,   0, &
  97, 165, 260, 293,   0, &
  68, 206, 259, 287,   0, &
  82, 207, 264, 292,   0, &
  98, 129, 249, 279,   0, &
  99, 202, 241, 299,   0, &
  53, 185, 240, 286,   0, &
  93, 123, 256, 285,   0, &
   7, 198, 254, 294,   0, &
  13, 199, 257, 275,   0, &
 100, 156, 251, 286,   0, &
 101, 169, 255, 274,   0, &
  31, 200, 260, 269,   0, &
  71, 207, 240, 297,   0, &
  83, 208, 241, 295,   0, &
 102, 196, 259, 269,   0, &
 103, 180, 261, 276,   0, &
  30, 134, 242, 283,   0, &
 104, 167, 251, 288,   0, &
 105, 182, 246, 280,   0, &
  72, 152, 258, 267,   0, &
 106, 160, 252, 291,   0, &
 101, 209, 234, 289,   0, &
 107, 151, 245, 270,   0, &
  76, 210, 251, 267,   0, &
  27, 190, 227, 277,   0, &
  60, 149, 254, 269,   0, &
  99, 177, 252, 294,   0, &
 108, 179, 240, 280,   0, &
  58, 187, 232, 281,   0, &
  51, 186, 236, 282,   0, &
 109, 210, 246, 273,   0, &
  87, 176, 237, 272,   0, &
  92, 198, 239, 284,   0, &
  59, 211, 245, 288,   0, &
   8, 158, 259, 281,   0, &
 110, 194, 264, 267,   0, &
  90, 209, 252, 273,   0, &
 111, 212, 252, 283,   0, &
 105, 128, 249, 275,   0, &
  88, 197, 259, 291,   0, &
  10, 200, 248, 271,   0, &
 112, 172, 253, 290,   0, &
 103, 132, 248, 282,   0, &
  69, 165, 251, 294,   0, &
  20, 206, 225, 263,   0, &
  18, 163, 259, 288,   0, &
  29, 203, 260, 285,   0, &
 111, 180, 230, 274,   0, &
  81, 213, 242, 295,   0, &
  78, 195, 255, 287,   0, &
 113, 162, 237, 294,   0, &
  28, 187, 244, 285,   0, &
  34, 194, 247, 290,   0, &
 114, 166, 252, 296,   0, &
   9, 164, 257, 265,   0, &
   6, 202, 257, 283,   0, &
   2, 203, 247, 282,   0, &
  89, 168, 239, 290,   0, &
 115, 206, 243, 279,   0, &
 116, 204, 255, 297, 298, &
  97, 214, 255, 291,   0, &
  86, 172, 243, 281,   0, &
 117, 186, 262, 289,   0, &
  96, 160, 238, 268, 297, &
  35, 170, 253, 283,   0, &
  40, 214, 239, 275,   0, &
  21, 192, 262, 276,   0, &
  98, 205, 254, 277,   0, &
   3, 192, 249, 293,   0, &
 113, 191, 250, 271,   0, &
  70, 133, 240, 279,   0, &
 100, 201, 239, 281,   0, &
  95, 175, 253, 286,   0, &
 115, 183, 245, 299,   0, &
  55, 154, 243, 288,   0, &
 107, 196, 218, 274,   0, &
  22, 201, 258, 272,   0, &
 118, 204, 254, 282,   0, &
  91, 138, 242, 284,   0, &
  54, 215, 249, 282,   0, &
  12, 188, 258, 280,   0, &
 114, 140, 247, 280,   0, &
  74, 184, 217, 267,   0, &
 104, 216, 255, 263,   0, &
 110, 191, 244, 295,   0, &
  52, 188, 242, 285,   0, &
 116, 136, 218, 289,   0, &
  23, 150, 214, 278,   0, &
 119, 185, 252, 277,   0, &
  79, 207, 251, 266,   0, &
 109, 127, 211, 269,   0, &
  25, 167, 242, 289,   0, &
  62, 193, 264, 287,   0, &
 118, 135, 229, 265,   0, &
 117, 213, 248, 297,   0, &
  33, 169, 256, 268,   0, &
  67, 147, 208, 268,   0, &
 108, 164, 261, 296,   0, &
 106, 153, 245, 280,   0, &
 120, 209, 243, 271,   0, &
 112, 162, 265, 285,   0, &
 119, 211, 258, 275,   0, &
  61, 144, 250, 290,   0, &
  14, 208, 250, 277,   0, &
  73, 195, 240, 273,   0, &
 102, 205, 235, 271,   0, &
  48, 184, 259, 285,   0, &
 120, 189, 261, 269, 297/

data nrw/    &
5,5,4,5,4,4,5,4,4,4,4,4,4,5,4,4,4,4,4,4, &
5,5,5,4,5,4,4,4,4,4,4,5,4,4,5,4,5,5,4,4, &
5,4,5,4,4,4,4,4,4,4,4,5,4,4,4,4,5,4,4,4, &
4,4,5,5,4,4,4,4,4,4,4,5,4,4,5,5,4,4,4,4, &
4,4,4,4,4,4,4,4,5,4,4,4,4,4,4,5,4,4,4,4, &
4,4,4,4,4,4,4,4,4,4,4,4,4,5,4,5,4,5,4,5, &
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
4,4,4,4,4,4,4,4,4,4,4,5,4,4,4,5,4,4,4,4, &
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, &
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5/

data ncw/    &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, &
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3, &
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, &
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, &
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, &
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,7,7, &
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, &
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, &
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7/ 

!ncw=3

toc=0
tov=0
tanhtoc=0
!write(*,*) llr
! initialize messages to checks
do j=1,M
  do i=1,nrw(j)
    toc(i,j)=llr((Nm(i,j)))
  enddo
enddo

ncnt=0

do iter=0,maxiterations

! Update bit log likelihood ratios (tov=0 in iteration 0).
  do i=1,N
    if( apmask(i) .ne. 1 ) then
      zn(i)=llr(i)+sum(tov(1:ncw(i),i))
    else
      zn(i)=llr(i)
    endif
  enddo

! Check to see if we have a codeword (check before we do any iteration).
  cw=0
  where( zn .gt. 0. ) cw=1
  ncheck=0
  do i=1,M
    synd(i)=sum(cw(Nm(1:nrw(i),i)))
    if( mod(synd(i),2) .ne. 0 ) ncheck=ncheck+1
!    if( mod(synd(i),2) .ne. 0 ) write(*,*) 'check ',i,' unsatisfied'
  enddo
!write(*,*) 'number of unsatisfied parity checks ',ncheck
  if( ncheck .eq. 0 ) then ! we have a codeword - reorder the columns and return it
!    niterations=iter
    codeword=cw(colorder+1)
    decoded=codeword(M+1:N)
    nerr=0
    do i=1,N
      if( (2*cw(i)-1)*llr(i) .lt. 0.0 ) nerr=nerr+1
    enddo
    niterations=nerr
    return
  endif

  if( iter.gt.0 ) then  ! this code block implements an early stopping criterion
    nd=ncheck-nclast
    if( nd .lt. 0 ) then ! # of unsatisfied parity checks decreased
      ncnt=0  ! reset counter
    else
      ncnt=ncnt+1
    endif
!    write(*,*) iter,ncheck,nd,ncnt
    if( ncnt .ge. 5 .and. iter .ge. 15 .and. ncheck .gt. 50) then
      niterations=-1
      return
    endif
  endif
  nclast=ncheck

! Send messages from bits to check nodes 
  do j=1,M
    do i=1,nrw(j)
      ibj=Nm(i,j)
      toc(i,j)=zn(ibj)  
!      do kk=1,ncw(ibj) ! subtract off what the bit had received from the check
      do kk=1,7 ! subtract off what the bit had received from the check
        if( Mn(kk,ibj) .eq. j ) then  
          toc(i,j)=toc(i,j)-tov(kk,ibj)
        endif
      enddo
    enddo
  enddo

! send messages from check nodes to variable nodes
  do i=1,M
    tanhtoc(1:5,i)=tanh(-toc(1:5,i)/2)
  enddo

  do j=1,N
    do i=1,ncw(j)
      ichk=Mn(i,j)  ! Mn(:,j) are the checks that include bit j
      Tmn=product(tanhtoc(1:nrw(ichk),ichk),mask=Nm(1:nrw(ichk),ichk).ne.j)
      call platanh(-Tmn,y)
!      y=atanh(-Tmn)
      tov(i,j)=2*y
    enddo
  enddo

enddo
niterations=-1
return
end subroutine bpdecode300
